package com.software.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.software.base.ResultInfo;
import com.software.bean.CategoryBean;
import com.software.dto.CategoryDto;
import com.software.dto.EditCategoryDto;
import com.software.dto.MidIconDto;
import com.software.dto.ThirdCategoryDto;
import com.software.service.CategoryService;
import com.software.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2024-03-07
 */
@Controller
@RequestMapping("/category")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;


    @GetMapping("/front/getMidIcon")
    @ResponseBody
    public ResultInfo getIcon() {
        List<CategoryBean> list = categoryService.queryMidIconInHomePage();
        return ResultInfo.success(list);

    }

    @GetMapping("/api/list")
    @ResponseBody
    public ResultInfo getIconList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        IPage<CategoryBean> categoryBeanIPage = categoryService.queryMidIconPagination(pageNum, pageSize);
        return ResultInfo.success((int) categoryBeanIPage.getTotal(), categoryBeanIPage.getRecords());
    }

    @PostMapping("/api/addMidIcon")
    @ResponseBody
    public ResultInfo addMidIcon(MidIconDto midIconDto) {
        int result = categoryService.addMidIcon(midIconDto);
        AssertUtil.isTrue(result == 0, "返回值异常");
        return ResultInfo.success();
    }

    @DeleteMapping("/api/deleteMidIcon/{id}")
    @ResponseBody
    public ResultInfo deleteMidIcon(@PathVariable Integer id) {
        int result = categoryService.deleteMidIcon(id);
        AssertUtil.isTrue(result == 0, "返回值异常");
        return ResultInfo.success();
    }


    @GetMapping("/api/getAllCategory")
    @ResponseBody
    public ResultInfo getAllCategory() {
        List<Map<String, Object>> mapList = categoryService.allCategory();
        return ResultInfo.success(mapList);
    }

    @GetMapping("/api/getAllFirstCategory")
    @ResponseBody
    public ResultInfo getAllFirstCategory() {
        List<Map<String, Integer>> maps = categoryService.queryAllFirstCategory();
        return ResultInfo.success(maps);
    }

    @PostMapping("/api/addSecondCategory")
    @ResponseBody
    public ResultInfo addSecondCategory(@RequestBody CategoryDto categoryDto) {
        Integer result = categoryService.addSecondCategory(categoryDto);
        AssertUtil.isTrue(result != 1, "添加失败了");
        return ResultInfo.success();
    }

    @GetMapping("/front/getAllCategory")
    @ResponseBody
    public ResultInfo getCategoryByFront() {
        List<Map<String, Object>> categoryByFront = categoryService.getCategoryByFront();
        AssertUtil.isTrue(categoryByFront.size() == 0, "分类信息返回异常 ");
        return ResultInfo.success(categoryByFront);
    }

    // 删除分类
    @DeleteMapping("/api/deleteCategory/{id}")
    @ResponseBody
    public ResultInfo deleteCategory(@PathVariable Integer id) {
        Integer result = categoryService.deleteCategory(id);
        AssertUtil.isTrue(result != 1, "删除操作失败");
        return ResultInfo.success();
    }


    // 添加三级分类
    @PostMapping("/api/addThirdCategory")
    @ResponseBody
    public ResultInfo addThirdCategory(ThirdCategoryDto thirdCategoryDto) {
        Integer result = categoryService.addThirdCategory(thirdCategoryDto);
        AssertUtil.isTrue(result != 1, "三级分类添加失败");
        return ResultInfo.success();
    }


    // 修改分类
    @GetMapping("/api/getCategoryById/{id}")
    @ResponseBody
    public ResultInfo getCategoryById(@PathVariable Integer id) {
        CategoryBean categoryBean = categoryService.getCategoryById(id);
        AssertUtil.isTrue(categoryBean == null, "返回值异常");
        return ResultInfo.success(categoryBean);
    }

    @PostMapping("/api/update")
    @ResponseBody
    public ResultInfo updateCategory(EditCategoryDto editCategoryDto) {
        Integer result = categoryService.updateCategory(editCategoryDto);
        AssertUtil.isTrue(result != 1, "更新失败");
        return ResultInfo.success();
    }


    /**
     * @description:前端类型选择器
     * @author: wyj
     * @date: 2024/3/28 14:46
     * @param:
     * @return:
     */
    @GetMapping("/front/getCategoryForRelease")
    @ResponseBody
    public ResultInfo getCategoryForRelease() {
        List<Map<String, Integer>> list = categoryService.getCategoryForRelease();
        AssertUtil.isTrue(list.size() == 0, "返回值异常");
        return ResultInfo.success(list);
    }

    /**
     * @description:获取分类名
     * @author: wyj
     * @date: 2024/4/3 14:17
     * @param:categoryId
     * @return:
     */
    @GetMapping("/front/getCategoryNameByCategoryId/{categoryId}")
    @ResponseBody
    public ResultInfo getCategoryNameByCategoryId(@PathVariable Integer categoryId) {
        CategoryBean categoryBean = categoryService.getCategoryById(categoryId);
        AssertUtil.isTrue(categoryBean == null, "返回值异常");
        return ResultInfo.success(categoryBean);
    }

}
